<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="手机号" prop="telphone">
            <Input v-model="formValidate.telphone" placeholder="请输入您的手机号"></Input>
        </FormItem>
        <FormItem label="学号" prop="USER_CODE">
            <Input v-model="formValidate.USER_CODE" placeholder="请输入您的学号"></Input>
        </FormItem>
        <FormItem label="新密码" prop="PASSWD">
            <Input type="password" v-model="formValidate.PASSWD" placeholder="请输入您的密码"></Input>
        </FormItem>
        <FormItem label="确认密码" prop="REPASSWD">
            <Input type="password" v-model="rePassword.REPASSWD" placeholder="请再次输入密码"></Input>
        </FormItem>
        
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">确认修改密码</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
			<Button @click="goback()" style="margin-left: 8px">返回主页</Button>
        </FormItem>
    </Form>
</template>
<script>
	import { password } from '@/api/lky/LkyStudentRegister'
	import { createModelObj } from '@/libs/util'
    export default {
        data () {
            return {
                formValidate: {
                    telphone: '',
                    USER_CODE: '',
                    id_card: '',
                    PASSWD: ''              
                },
				rePassword: {
					REPASSWD: ''
				},
                ruleValidate: {
                    USER_NAME: [
                        { required: true, message: '名字不能为空', trigger: 'blur' }
                    ],
                    telphone: [
                        { required: true, message: '手机号不能为空', trigger: 'blur' },
						{pattern: /^1[34578]\d{9}$/, message: '手机格式有误！', trigger: 'blur'}
                    ],
                    USER_CODE: [
                        { required: true, message: '学号不能为空', trigger: 'blur' }
                    ],
                    PASSWD: [
                        { required: true, message: '密码不能为空', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid && this.formValidate.PASSWD == this.rePassword.REPASSWD) {
						password(Object.assign({}, createModelObj(this.formValidate, 'student'))).then(
						  res => {
						    if (res) {
						      this.$emit('addRow', res.row)
						      this.$emit('input', false)
						      this.handleReset(name)
						    }
						  }
						)
                        console.eval(res)
                    } else if(this.formValidate.PASSWD != this.rePassword.REPASSWD) {
                        this.$Message.error('两遍密码不一致!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
			goback(){
				window.location.href='/home'
			}
        }
    }
</script>